﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>SimpleInvoiceQuery Sample</title>
    <script language="javascript" type="text/javascript" src="../../Script/GrammarIntelliForJS.js"></script>
    <script language="javascript" type="text/javascript">
        var sDatePattern ="^\\d\\d/\\d\\d/\\d\\d\\d\\d$", oGIMachine = new GIMachine("SimpleInvoiceQuery"), oStartDate = null, oEndDate = null, oInvoice = null, oFrom = null, oTo = null;
        
        // EndDate node
        oEndDate = new GIMachineWordState(sDatePattern /* Pattern */,
                                                     false /* This is not a literal text/pattern */,
                                                     7 /*can be any number for tracking*/,
                                                     "EndDate" /*This name will can be used to capture result*/,
                                                     true /* final state of the query */);
        // StartDate
        oStartDate = new GIMachineWordState(sDatePattern /* Pattern */,
                                                     false /* This is not a literal text/pattern */,
                                                     5 /*can be any number for tracking*/,
                                                     "StartDate" /*This name will can be used to capture result*/,
                                                     false /* not a final state of the query */);




        oGIMachine.States[0] = new GIMachineWordState("display" /* Pattern */,
                                                     true /* This is a literal text/pattern */,
                                                     0 /*can be any number for tracking*/,
                                                     "" /*Literal shouldn't carry a name*/,
                                                     false /* final state of the query */);
                                                     
        oGIMachine.States[1] = new GIMachineWordState("show" /* Pattern */,
                                                     true /* This is a literal text/pattern */,
                                                     1 /*can be any number for tracking*/,
                                                     "" /*Literal shouldn't carry a name*/,
                                                     false /* final state of the query */);
                                                     
        oGIMachine.States[2] = new GIMachineWordState("list" /* Pattern */,
                                                     true /* This is a literal text/pattern */,
                                                     2 /*can be any number for tracking*/,
                                                     "" /*Literal shouldn't carry a name*/,
                                                     false /* final state of the query */);

        oInvoice = new GIMachineWordState("invoice" /* Pattern */,
                                                     true /* This is a literal text/pattern */,
                                                     3 /*can be any number for tracking*/,
                                                     "" /*Literal shouldn't carry a name*/,
                                                     false /* final state of the query */);

        oFrom = new GIMachineWordState("from" /* Pattern */,
                                                     true /* This is a literal text/pattern */,
                                                     4 /*can be any number for tracking*/,
                                                     "" /*Literal shouldn't carry a name*/,
                                                     false /* final state of the query */);

        oTo = new GIMachineWordState("to" /* Pattern */,
                                                     true /* This is a literal text/pattern */,
                                                     6 /*can be any number for tracking*/,
                                                     "" /*Literal shouldn't carry a name*/,
                                                     false /* final state of the query */);                                            
                                                     

        oGIMachine.States[0].NextStates[0] = oInvoice; // linking display -> invoice
        oGIMachine.States[1].NextStates[0] = oInvoice; // linking show -> invoice
        oGIMachine.States[2].NextStates[0] = oInvoice; // linking list -> invoice

        oInvoice.NextStates[0] = oFrom; // linking invoice -> from

        oFrom.NextStates[0] = oStartDate;

        oStartDate.NextStates[0] = oTo;

        oTo.NextStates[0] = oEndDate;
        
        
       
                                                     

        function ShowMachineInfo() {
            // Its good to see what your GIMachine looks like:
            // document.getElementById("txtOutput").value = oGIMachine.ToXML();
        }

        function ShowIntellisense() {
            var sText = document.getElementById("txtUserInput").value, oDiv = document.getElementById("divSuggestions"), sHTML = "", i = 0, oTrace = document.getElementById("txtOutput"), arrSuggestions = null;
            //arrSuggestions = oGIMachine.Suggestions(sText, oTrace); // With trace ... good for debugging
            arrSuggestions = oGIMachine.Suggestions(sText); // no tracing

            if (arrSuggestions != null && arrSuggestions.length > 0) {
                
                for (i = 0; i < arrSuggestions.length; i++) {
                    if (arrSuggestions[i].IsLiteral) {
                        sHTML += "<b>L:</b> &nbsp;" + arrSuggestions[i].Pattern + "<br>";
                    } else {
                        sHTML += "<b>R:</b> &nbsp;" + arrSuggestions[i].Pattern + "  [" + arrSuggestions[i].Name + "]<br>";
                    }
                }
                oDiv.innerHTML = sHTML;
            } else {
                oTrace.value = "0 suggestions for '" + sText + "'";
                oDiv.innerHTML = "";
            }
        }

        function ParseText() {
            var sText = document.getElementById("txtUserInput").value, oDiv = document.getElementById("divSuggestions"), oTrace = document.getElementById("txtOutput"), oMatchCollection = null;
            //oMatchCollection = oGIMachine.ProcessText(sText, oTrace);
            oMatchCollection = oGIMachine.ProcessText(sText);

            if (oMatchCollection != null) {
                var sStartDate = "", sEndDate = "";
                oTrace.value = oMatchCollection.ToXML();

                // Now, its time to get the result ... lets capture the "CustomerName" that user entered
                sStartDate = oMatchCollection.NamedMatches["StartDate"].Value;
                sEndDate = oMatchCollection.NamedMatches["EndDate"].Value;
                // At this point you can do whatever you want with the data ... we'r just displaying it off.
                oDiv.innerHTML = "Result: Captured StartDate = '<b>" + sStartDate + "'</b>, EndDate = '<b>" + sEndDate + "'</b>";
            } else {
                oTrace.value = "No matches...";
            }
        }
        
    </script>
</head>
<body onload="ShowMachineInfo();">
    <h1>Simple Invoice Query - Sample</h1>
    <h3>Try typing according to pattern: <font color="blue"><i>(display|show|list) invoice from &lt;StartingDate&gt; to &lt;EndDate&gt;</i></font></h3>
    <input type="text" id="txtUserInput" onkeyup="ShowIntellisense()" size="60"  />&nbsp;<input type="button" value="Parse" onclick="ParseText();" /><br />
    <div id="divSuggestions"></div>
    
    <textarea id="txtOutput" rows="30" cols="80"></textarea>
</body>
</html>
